package colab.persistencia;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import colab.dominio.Predio;
import colab.dominio.Sala;

public class SalasDAO {
	
	public ArrayList<Sala> listarSalas(Predio predio) throws SQLException{
		ArrayList<Sala> salas = new ArrayList<Sala>();
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		
		try {
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement("select * from labs where predio like '"+predio.getId_predio()+"'");
			rs = stmt.executeQuery();
			while(rs.next()){
				Sala sala = new Sala();
				sala.setId_sala(rs.getInt("Id"));
				sala.setPredio(rs.getString("predio"));
				salas.add(sala);
				}
			
	        
			} 
		catch (SQLException e) {
			e.printStackTrace();
			}
		 finally {
			 
			 
			 	rs.close();
				stmt.close();
				conn.close();
			   }
		
		return salas;
		}
	
	public Sala getSala(int idSala) throws SQLException{
		Sala sala = null;
		Connection conn = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		
		try {
			conn = Conexao.getConnection();
			stmt = conn.prepareStatement("select * from labs where Id = '"+idSala+"'");
			rs = stmt.executeQuery();
			
			if(rs.next()){
				sala = new Sala();
				sala.setId_sala(rs.getInt("Id"));
				sala.setPredio(rs.getString("predio"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		 finally {
			 	
					rs.close();
					stmt.close();
					conn.close();
				
			   }
		
		return sala;
		
		
	}
}
